package com.lapian.designmode.observer.improve;

import com.lapian.designmode.observer.ZhangShangApp;

import java.util.ArrayList;

/**
 * 创建日期：2021/11/12 10:50
 *
 * @author tony.sun
 * 类说明：实时数据天气
 */

public class WeatherDate implements Subject{

    private float temperature;
    private float pressure;
    private float humidity;
    private ArrayList<Observer> observers;

    public float getTemperature() {
        return temperature;
    }

    public float getPressure() {
        return pressure;
    }

    public float getHumidity() {
        return humidity;
    }

    public WeatherDate() {
        observers=new ArrayList<>();
    }

    public void setData(float temperature, float pressure, float humidity) {
        this.temperature=temperature;
        this.pressure=pressure;
        this.humidity=humidity;
        dateChange();
    }

    private void dateChange() {
        notifyObservers();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void unRegisterObserver(Observer observer) {
        if (observer!=null) {
            observers.remove(observer);
        }
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(getTemperature(),getPressure(),getHumidity());
        }
    }
}
